package com.grass.p019lv.activity;

import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.TextView;
import com.alibaba.android.arouter.launcher.ARouter;
import com.androidsa.bttsddka.R;
import com.androidx.p013lv.base.Constance;
import com.androidx.p013lv.base.bean.SearchBean;
import com.androidx.p013lv.base.http.BaseRes;
import com.androidx.p013lv.base.http.HttpUtils;
import com.androidx.p013lv.base.http.UrlManager;
import com.androidx.p013lv.base.http.callback.HttpCallback;
import com.androidx.p013lv.base.interfaces.Key;
import com.androidx.p013lv.base.p015ui.BaseActivity;
import com.androidx.p013lv.base.utils.SpUtils;
import com.androidx.p013lv.base.utils.ToastUtils;
import com.grass.p019lv.adapter.AdapterSearchRecommend;
import com.grass.p019lv.databinding.ActivitySearchBinding;
import com.gyf.immersionbar.ImmersionBar;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;
import com.zhy.view.flowlayout.TagView;
import java.util.List;

/* renamed from: com.grass.lv.activity.SearchActivity */
public class SearchActivity extends BaseActivity<ActivitySearchBinding> {
    String HOT_LIST = "HotList";
    AdapterSearchRecommend adapter;

    @Override // com.androidx.p013lv.base.p015ui.BaseActivity
    public int setLayout() {
        return R.layout.activity_search;
    }

    /* access modifiers changed from: protected */
    @Override // com.androidx.p013lv.base.p015ui.BaseActivity
    public void initView() {
        super.initView();
        ((ActivitySearchBinding) this.binding).tvCancel.setOnClickListener(new View.OnClickListener() {
            /* class com.grass.p019lv.activity.SearchActivity.View$OnClickListenerC28871 */

            public void onClick(View view) {
                SearchActivity.this.finish();
            }
        });
        setHistory();
        ((ActivitySearchBinding) this.binding).edSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            /* class com.grass.p019lv.activity.SearchActivity.C28882 */

            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (SearchActivity.this.isOnClick() || i != 3) {
                    return false;
                }
                String trim = ((ActivitySearchBinding) SearchActivity.this.binding).edSearch.getText().toString().trim();
                if (TextUtils.isEmpty(trim)) {
                    return true;
                }
                SpUtils.getInstance().setHistory(trim);
                ARouter.getInstance().build(Constance.SEARCH_RESULT_ACTIVITY).withString(Key.TXT, trim).navigation();
                return true;
            }
        });
        ((ActivitySearchBinding) this.binding).gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            /* class com.grass.p019lv.activity.SearchActivity.C28893 */

            @Override // android.widget.AdapterView.OnItemClickListener
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long j) {
                if (!SearchActivity.this.isOnClick()) {
                    ARouter.getInstance().build(Constance.SEARCH_RESULT_ACTIVITY).withString(Key.TXT, SearchActivity.this.adapter.getItem(i).getHotTitle()).navigation();
                }
            }
        });
        ((ActivitySearchBinding) this.binding).ivDelete.setOnClickListener(new View.OnClickListener() {
            /* class com.grass.p019lv.activity.SearchActivity.View$OnClickListenerC28904 */

            public void onClick(View view) {
                SpUtils.getInstance().clearHistory();
                SearchActivity.this.setHistory();
            }
        });
    }

    /* access modifiers changed from: protected */
    @Override // com.androidx.p013lv.base.p015ui.BaseActivity
    public void initData() {
        super.initData();
        this.adapter = new AdapterSearchRecommend(getActivity());
        ((ActivitySearchBinding) this.binding).gridView.setAdapter((ListAdapter) this.adapter);
        getHotWord();
    }

    /* access modifiers changed from: protected */
    @Override // androidx.fragment.app.FragmentActivity
    public void onResume() {
        super.onResume();
        setHistory();
        showSoftInputFromWindow();
    }

    public void showSoftInputFromWindow() {
        ((ActivitySearchBinding) this.binding).edSearch.setFocusable(true);
        ((ActivitySearchBinding) this.binding).edSearch.setFocusableInTouchMode(true);
        ((ActivitySearchBinding) this.binding).edSearch.requestFocus();
        getWindow().setSoftInputMode(5);
    }

    /* access modifiers changed from: package-private */
    public void setHistory() {
        List<String> history = SpUtils.getInstance().getHistory();
        if (history == null || history.size() <= 0) {
            ((ActivitySearchBinding) this.binding).history.setVisibility(8);
            ((ActivitySearchBinding) this.binding).rlHistory.setVisibility(8);
            return;
        }
        ((ActivitySearchBinding) this.binding).history.setVisibility(0);
        ((ActivitySearchBinding) this.binding).rlHistory.setVisibility(0);
        ((ActivitySearchBinding) this.binding).history.setAdapter(new TagAdapter<String>(history) {
            /* class com.grass.p019lv.activity.SearchActivity.C28915 */

            public View getView(FlowLayout flowLayout, int i, String str) {
                TextView textView = (TextView) LayoutInflater.from(flowLayout.getContext()).inflate(R.layout.item_search_history, (ViewGroup) ((ActivitySearchBinding) SearchActivity.this.binding).history, false);
                textView.setText(str);
                return textView;
            }
        });
        ((ActivitySearchBinding) this.binding).history.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            /* class com.grass.p019lv.activity.SearchActivity.C28926 */

            @Override // com.zhy.view.flowlayout.TagFlowLayout.OnTagClickListener
            public boolean onTagClick(View view, int i, FlowLayout flowLayout) {
                if (SearchActivity.this.isOnClick()) {
                    return false;
                }
                String charSequence = ((TextView) ((TagView) view).getTagView()).getText().toString();
                ((ActivitySearchBinding) SearchActivity.this.binding).edSearch.setText(charSequence);
                ARouter.getInstance().build(Constance.SEARCH_RESULT_ACTIVITY).withString(Key.TXT, charSequence).navigation();
                return false;
            }
        });
    }

    /* access modifiers changed from: protected */
    @Override // com.androidx.p013lv.base.p015ui.BaseActivity
    public void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this).titleBar(((ActivitySearchBinding) this.binding).toolbar).init();
    }

    public void getHotWord() {
        HttpUtils.getInsatance().get(UrlManager.getInsatance().hotList(), new HttpCallback<BaseRes<SearchBean>>(this.HOT_LIST) {
            /* class com.grass.p019lv.activity.SearchActivity.C28937 */

            public void onLvSuccess(BaseRes<SearchBean> baseRes) {
                if (baseRes.getCode() == 200) {
                    SearchActivity.this.adapter.setList(baseRes.getData().getList());
                } else {
                    ToastUtils.getInstance().showWeak(baseRes.getMsg());
                }
            }
        });
    }

    /* access modifiers changed from: protected */
    @Override // androidx.appcompat.app.AppCompatActivity, androidx.fragment.app.FragmentActivity, com.androidx.p013lv.base.p015ui.BaseActivity
    public void onDestroy() {
        HttpUtils.getInsatance().cancelTag(this.HOT_LIST);
        super.onDestroy();
    }
}
